package com.fenghongzhang.day009view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

public class SlidingMenu extends HorizontalScrollView {
    private boolean runOnce = true;
    private LinearLayout mMenu;
    private LinearLayout mContent;
    private int mWindowWidth;
    private int mMenuMeasureWidth;

    public SlidingMenu(Context context) {
        super(context);
        init();
    }

    public SlidingMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SlidingMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public SlidingMenu(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getRealMetrics(displayMetrics);
        mWindowWidth = displayMetrics.widthPixels;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        if (runOnce) {
            runOnce = false;
            LinearLayout linearLayout = (LinearLayout)getChildAt(0);
            mMenu = (LinearLayout)linearLayout.getChildAt(0);
            mContent = (LinearLayout)linearLayout.getChildAt(1);
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams )mContent.getLayoutParams();
            params.width = mWindowWidth;
            mContent.setLayoutParams(params);

            mMenuMeasureWidth = mMenu.getMeasuredWidth();
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed) {
            ViewTreeObserver viewTreeObserver = getViewTreeObserver();
            viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    scrollTo(mMenuMeasureWidth, 0);
                }
            });
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        super.onTouchEvent(ev);
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            int scrollX = getScrollX();
            if (scrollX < mMenuMeasureWidth / 2) {
                smoothScrollTo(0, 0);
            }
            else {
                smoothScrollTo(mMenuMeasureWidth, 0);
            }
        }
        return true;
    }
}
